package com.xbongbong.paas.connection.connector.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.connection.connector.help.ConnectorHelper;
import com.xbongbong.paas.connection.pojo.connector.ContractInfoPojo;
import com.xbongbong.paas.connection.pojo.connector.DdPaymentSheetStandardActionPojo;
import com.xbongbong.paas.connection.pojo.connector.OrderSettlementPojo;
import com.xbongbong.paas.connection.pojo.connector.PaymentInfoPojo;
import com.xbongbong.paas.connection.pojo.connector.PaymentSheetStandardFormatPojo;
import com.xbongbong.paas.connection.pojo.connector.StmtPojo;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.PaasFormExplainEntity;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.elasticsearch.util.EsHelper;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.enums.PlatFormEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.ItemPoJo;
import com.xbongbong.paas.help.ProFormHelp;
import com.xbongbong.paas.model.PaasFormExplainModel;
import com.xbongbong.paas.model.PaasFormModel;
import com.xbongbong.paas.pojo.dto.FormDataAddDTO;
import com.xbongbong.paas.pojo.dto.FormDataDeleteBatchDTO;
import com.xbongbong.paas.pojo.dto.FormDataUpdateDTO;
import com.xbongbong.paas.pojo.dto.FormDataUpdateGetDTO;
import com.xbongbong.paas.pojo.vo.FormDataAddVO;
import com.xbongbong.paas.pojo.vo.FormDataUpdateGetVO;
import com.xbongbong.paas.pojo.vo.FormDataUpdateVO;
import com.xbongbong.paas.service.PaasFormDataService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.UserVO;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.domain.entity.ext.ContractEntityExt;
import com.xbongbong.saas.domain.entity.ext.PaymentEntityExt;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.PaymentEnum;
import com.xbongbong.saas.enums.business.PaymentSheetEnum;
import com.xbongbong.saas.model.ContractModel;
import com.xbongbong.saas.model.PaymentModel;
import com.xbongbong.saas.service.ListBatchService;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
import static org.elasticsearch.index.query.QueryBuilders.termsQuery;

@Service("paymentSheetStandardConnector")
public class PaymentSheetStandardConnector extends AbstractStandardConnector {

    private static final Logger LOG = LoggerFactory.getLogger(PaymentSheetStandardConnector.class);

    @Resource
    private EsHelper esHelper;
    @Resource
    private ConnectorHelper connectorHelper;
    @Resource
    private PaasFormModel paasFormModel;
    @Resource
    private PaasFormExplainModel paasFormExplainModel;
    @Resource
    private ProFormHelp proFormHelp;
    @Resource
    private ContractModel contractModel;
    @Resource
    private PaymentModel paymentModel;
    @Resource
    private PaasFormDataService paasFormDataService;
    @Resource
    private ListBatchService listBatchService;

    @Override
    protected PaasFormDataEntityExt getEntity(String corpid, JSONObject jsonObject) throws XbbException {
        DdPaymentSheetStandardActionPojo ddPaymentSheetStandardActionPojo = jsonObject.toJavaObject(DdPaymentSheetStandardActionPojo.class);
        PaymentSheetStandardFormatPojo data = ddPaymentSheetStandardActionPojo.getData();
        String serialNo = data.getCode();
        PaasFormDataEntityExt paymentSheet = getPaymentSheetBySerialNo(corpid, serialNo);
        return paymentSheet;
    }

    @Override
    protected void addStandard(String corpid, UserVO userVo, JSONObject jsonObject) throws XbbException {
        DdPaymentSheetStandardActionPojo ddPaymentSheetStandardActionPojo = jsonObject.toJavaObject(DdPaymentSheetStandardActionPojo.class);
        PaymentSheetStandardFormatPojo data = ddPaymentSheetStandardActionPojo.getData();
        String modifiedAppId = ddPaymentSheetStandardActionPojo.getDdDataModifiedAppId();
        String serialNo = data.getCode();
        String remark = data.getRemark();
        String customerNumber = data.getCustomerCode();
        String customerName = data.getCustomerName();
        Long billDate = data.getBillDate();
        Long paymentTime = billDate/1000L;
        List<StmtPojo> stmtList = data.getStmtList();
        List<OrderSettlementPojo> orderSettlementList = data.getOrderSettlementList();
        String amountStr = data.getAmount();
        Double amount = Double.parseDouble(amountStr);
        //金蝶测试应用appId：61901 金蝶正式应用appId：61790
        if (Objects.equals(modifiedAppId, "61901") || Objects.equals(modifiedAppId, "61790")) {
            //金蝶中订单收款金额逻辑特殊逻辑
            jindieOrderSet(orderSettlementList, amount);
        }
        JSONObject customerJson = connectorHelper.getCustomerObject(corpid, customerNumber);
        JSONArray belongArray = connectorHelper.getUserArray(corpid, data.getEmpCode());
        List<String> contractSerialNoList = new ArrayList<>();
        for (OrderSettlementPojo orderSettlementPojo : orderSettlementList) {
            String orderNumber = orderSettlementPojo.getOrderCode();
            contractSerialNoList.add(orderNumber);
        }
        PaasFormEntityExt paasForm = paasFormModel.getByBusinessType(XbbRefTypeEnum.PAYMENT_SHEET.getCode(), corpid);
        Long formId = paasForm.getId();
        PaasFormExplainEntity paasFormExplainEntity = paasFormExplainModel.getByFormId(formId, corpid);
        List<FieldAttrEntity> explainList = JSONArray.parseArray(paasFormExplainEntity.getExplains(), FieldAttrEntity.class);
        Map<String, FieldAttrEntity> explainMap = new HashMap<>();
        explainList.forEach(item -> {
            explainMap.put(item.getAttr(), item);
        });
        JSONObject dataList = new JSONObject();
        dataList.put(PaymentSheetEnum.CUSTOMER_ID.getAttr(), customerJson);
        JSONArray contractArray = connectorHelper.getContractArrayBySerialNo(corpid, contractSerialNoList);
        if (Objects.isNull(contractArray) || contractArray.isEmpty()) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100404, "合同不存在或已删除");
        }
        JSONArray customerArray = new JSONArray();
        customerArray.add(customerJson);
        dataList.put(PaymentSheetEnum.CUSTOMER_ID.getAttr(), customerArray);
        dataList.put(PaymentSheetEnum.CONTRACT.getAttr(), contractArray);
        dataList.put(PaymentSheetEnum.BELONG_ID.getAttr(), belongArray);
        dataList.put(PaymentSheetEnum.PAYMENT_TIME.getAttr(), paymentTime);
        dataList.put(PaymentSheetEnum.AMOUNT.getAttr(), amount);
        dataList.put(PaymentSheetEnum.MEMO.getAttr(), remark);
        List<ItemPoJo> itemPoJoList = proFormHelp.handleFundAccountItems(corpid, XbbRefTypeEnum.PAYMENT_SHEET.getCode());
        LOG.info("itemPoJoList:" + itemPoJoList);
        dataList.put(PaymentSheetEnum.FUND_ACCOUNT.getAttr(), connectorHelper.getComboObject(itemPoJoList, data.getSettleAccount()));
        dataList.put(PaymentSheetEnum.PAY_METHOD.getAttr(), connectorHelper.getComboObject(explainMap.get(PaymentSheetEnum.PAY_METHOD.getAttr()), data.getPayType()));
        JSONArray amountDetails = getAmountDetails(corpid, dataList, stmtList, orderSettlementList);
        dataList.put(PaasConstant.AMOUNTDETAIL, amountDetails);

        FormDataAddDTO formDataSaveDTO = new FormDataAddDTO();
        formDataSaveDTO.setFormId(formId);
        formDataSaveDTO.setAppId(paasForm.getAppId());
        formDataSaveDTO.setMenuId(paasForm.getMenuId());
        formDataSaveDTO.setBusinessType(XbbRefTypeEnum.PAYMENT_SHEET.getCode());
        formDataSaveDTO.setPlatform(PlatFormEnum.WEB.getValue());
        formDataSaveDTO.setSaasMark(SaasMarkEnum.SAAS.getCode());
        formDataSaveDTO.setSerialNo(serialNo);
        formDataSaveDTO.setCorpid(corpid);
        formDataSaveDTO.setUserId(userVo.getUserId());
        formDataSaveDTO.setDataList(dataList);
        formDataSaveDTO.setLoginUser(userVo);
        LOG.info("paasFormDataService.add");
        try {
            FormDataAddVO formDataAddVO = paasFormDataService.add(formDataSaveDTO);
        } catch (XbbException e) {
            e.printStackTrace();
            throw e;
        }
    }

    /**
     * 将参数转化成销帮帮中的明细
     * @param corpid
     * @param dataList
     * @param stmtList
     * @param orderSettlementList
     * @return
     */
    private JSONArray getAmountDetails(String corpid, JSONObject dataList, List<StmtPojo> stmtList, List<OrderSettlementPojo> orderSettlementList) {
        JSONArray amountDetails = new JSONArray();
        if (Objects.nonNull(stmtList) && !stmtList.isEmpty()) {
            Map<String, PaymentInfoPojo> paymentInfoMap = getPaymentInfoMap(corpid, stmtList);
            Set<String> keySet = paymentInfoMap.keySet();
            for (String key : keySet) {
                JSONObject amountDetail = (JSONObject) dataList.clone();
                PaymentInfoPojo paymentInfoPojo = paymentInfoMap.get(key);

                //关联合同
                JSONArray contractArray = new JSONArray();
                JSONObject contractJson = new JSONObject();
                contractJson.put("id", paymentInfoPojo.getContractId());
                contractJson.put("name", paymentInfoPojo.getContractSerialNo());
                contractArray.add(contractJson);
                amountDetail.put(PaymentSheetEnum.CONTRACT.getAttr(), contractArray);

                //关联回款计划
                JSONArray paymentArray = new JSONArray();
                JSONObject paymentJson = new JSONObject();
                paymentJson.put("id", paymentInfoPojo.getId());
                paymentJson.put("name", paymentInfoPojo.getSerialNo());
                paymentArray.add(paymentJson);
                amountDetail.put(PaymentSheetEnum.PAYMENT.getAttr(), contractArray);
                amountDetail.put(PaymentSheetEnum.AMOUNT.getAttr(), paymentInfoPojo.getPaymentAmount());
                amountDetails.add(amountDetail);
            }
        } else {
            Map<String, ContractInfoPojo> contractInfoMap = getContractInfoMap(corpid, orderSettlementList);
            Set<String> keySet = contractInfoMap.keySet();
            for (String key : keySet) {
                JSONObject amountDetail = (JSONObject) dataList.clone();
                ContractInfoPojo contractInfoPojo = contractInfoMap.get(key);
                //关联合同
                JSONArray contractArray = new JSONArray();
                JSONObject contractJson = new JSONObject();
                contractJson.put("id", contractInfoPojo.getId());
                contractJson.put("name", contractInfoPojo.getSerialNo());
                contractArray.add(contractJson);
                amountDetail.put(PaymentSheetEnum.CONTRACT.getAttr(), contractArray);
                amountDetail.put(PaymentSheetEnum.AMOUNT.getAttr(), contractInfoPojo.getPaymentAmount());
                amountDetails.add(amountDetail);
            }
        }
        return amountDetails;
    }

    private Map<String, PaymentInfoPojo> getPaymentInfoMap(String corpid, List<StmtPojo> stmtList) {
        Map<String, StmtPojo> stmtMap = new HashMap<>();
        List<String> serialNoIn = new ArrayList<>();
        for (StmtPojo stmt : stmtList) {
            String stmtCode = stmt.getStmtCode();
            serialNoIn.add(stmt.getStmtCode());
            stmtMap.put(stmtCode, stmt);
        }
        Map<String, Object> param = new HashMap<>();
        param.put("corpid", corpid);
        param.put("serialNoIn", serialNoIn);
        param.put("del", 0);
        List<PaymentEntityExt> paymentList = paymentModel.findEntitys(param);

        Map<String, PaymentInfoPojo> paymentInfoMap = new HashMap<>();
        for (PaymentEntityExt payment : paymentList) {
            String serialNo = payment.getSerialNo();
            StmtPojo stmt = stmtMap.get(serialNo);
            JSONObject data = payment.getData();
            PaymentInfoPojo paymentInfoPojo = new PaymentInfoPojo();
            paymentInfoPojo.setId(payment.getId());
            paymentInfoPojo.setContractId(data.getLong(PaymentEnum.CONTRACT.getAttr()));
            paymentInfoPojo.setContractSerialNo(data.getString(PaymentEnum.CONTRACT_LINK_TEXT.getAttr()));
            paymentInfoPojo.setSerialNo(payment.getSerialNo());
            paymentInfoPojo.setAmount(Double.parseDouble(stmt.getStmtAmount()));
            paymentInfoPojo.setPaymentAmount(Double.parseDouble(stmt.getStmtPaymentAmount()));
            paymentInfoMap.put(serialNo, paymentInfoPojo);
        }
        return paymentInfoMap;
    }

    private Map<String, ContractInfoPojo> getContractInfoMap(String corpid, List<OrderSettlementPojo> orderSettlementList) {
        Map<String, ContractInfoPojo> contractInfoMap = new HashMap<>();
        Map<String, OrderSettlementPojo> contractMap = new HashMap<>();
        List<String> serialNoIn = new ArrayList<>();
        for (OrderSettlementPojo orderSettlement : orderSettlementList) {
            String orderCode = orderSettlement.getOrderCode();
            serialNoIn.add(orderCode);
            contractMap.put(orderCode, orderSettlement);
        }
        Map<String, Object> param = new HashMap<>();
        param.put("corpid", corpid);
        param.put("serialNoIn", serialNoIn);
        param.put("del", 0);
        List<ContractEntityExt> contractList = contractModel.findEntitysWithoutSub(param);
        for (ContractEntityExt contract : contractList) {
            String serialNo = contract.getSerialNo();
            OrderSettlementPojo orderSettlementPojo = contractMap.get(serialNo);
            ContractInfoPojo contractInfoPojo = new ContractInfoPojo();
            contractInfoPojo.setId(contract.getId());
            contractInfoPojo.setSerialNo(serialNo);
            contractInfoPojo.setAmount(Double.parseDouble(orderSettlementPojo.getSettlementAmount()));
            contractInfoPojo.setPaymentAmount(Double.parseDouble(orderSettlementPojo.getOrderPaymentAmount()));
            contractInfoMap.put(serialNo, contractInfoPojo);
        }
        return contractInfoMap;
    }

    @Override
    protected void updateStandard(String corpid, UserVO userVo, JSONObject jsonObject, PaasFormDataEntityExt paasFormData) throws XbbException {
        DdPaymentSheetStandardActionPojo ddPaymentSheetStandardActionPojo = jsonObject.toJavaObject(DdPaymentSheetStandardActionPojo.class);
        Long modifiedTime = ddPaymentSheetStandardActionPojo.getDdDataGmtModified();
        PaymentSheetStandardFormatPojo data = ddPaymentSheetStandardActionPojo.getData();
        String serialNo = data.getCode();
        String remark = data.getRemark();
        JSONObject dbPaymentSheetDataList = getDbPaymentSheetDataList(paasFormData, userVo);
        JSONArray amountdetails = dbPaymentSheetDataList.getJSONArray(PaasConstant.AMOUNTDETAIL);
        for (Object amountdetail : amountdetails) {
            JSONObject amountdetailJson = (JSONObject)amountdetail;
            amountdetailJson.put(PaymentSheetEnum.MEMO.getAttr(), remark);
        }
        dbPaymentSheetDataList.put(PaymentSheetEnum.MEMO.getAttr(), remark);
        if (paasFormData.getUpdateTime() < modifiedTime) {
            FormDataUpdateDTO formDataUpdateDTO = new FormDataUpdateDTO();
            formDataUpdateDTO.setFormId(paasFormData.getFormId());
            formDataUpdateDTO.setAppId(paasFormData.getAppId());
            formDataUpdateDTO.setMenuId(paasFormData.getMenuId());
            formDataUpdateDTO.setBusinessType(XbbRefTypeEnum.PAYMENT_SHEET.getCode());
            formDataUpdateDTO.setSubBusinessType(XbbRefTypeEnum.PAYMENT_SHEET.getCode());
            formDataUpdateDTO.setPlatform(PlatFormEnum.WEB.getValue());
            formDataUpdateDTO.setSaasMark(SaasMarkEnum.SAAS.getCode());
            formDataUpdateDTO.setSerialNo(paasFormData.getSerialNo());
            formDataUpdateDTO.setDataList(dbPaymentSheetDataList);
            formDataUpdateDTO.setCorpid(paasFormData.getCorpid());
            formDataUpdateDTO.setDataId(paasFormData.getDataId());
            formDataUpdateDTO.setUserId(userVo.getUserId());
            formDataUpdateDTO.setLoginUser(userVo);
            LOG.info("paasFormDataService.update");
            try {
                FormDataUpdateVO formDataUpdateVO = paasFormDataService.update(formDataUpdateDTO);
            } catch (XbbException e) {
                e.printStackTrace();
                throw e;
            }

        } else {
            LOG.info("存在回款单，但回款单的更新时间>=执行动作接收到的更新时间");
        }
    }

    /**
     * 模拟点击编辑
     * @param paymentSheet
     * @param loginUser
     * @return
     * @throws XbbException
     */
    private JSONObject getDbPaymentSheetDataList(PaasFormDataEntityExt paymentSheet, UserVO loginUser) throws XbbException {
        FormDataUpdateGetDTO formDataGetDTO = new FormDataUpdateGetDTO();
        formDataGetDTO.setAppId(paymentSheet.getAppId());
        formDataGetDTO.setMenuId(paymentSheet.getMenuId());
        formDataGetDTO.setFormId(paymentSheet.getFormId());
        formDataGetDTO.setCorpid(paymentSheet.getCorpid());
        formDataGetDTO.setSaasMark(SaasMarkEnum.SAAS.getCode());
        formDataGetDTO.setPlatform(PlatFormEnum.WEB.getValue());
        formDataGetDTO.setBusinessType(XbbRefTypeEnum.PAYMENT_SHEET.getCode());
        formDataGetDTO.setSubBusinessType(XbbRefTypeEnum.PAYMENT_SHEET.getCode());
        formDataGetDTO.setLoginUser(loginUser);
        formDataGetDTO.setDataId(paymentSheet.getDataId());
        FormDataUpdateGetVO formDataUpdateGetVO = paasFormDataService.updateGet(formDataGetDTO);
        return formDataUpdateGetVO.getData().getData();
    }

    @Override
    protected void deleteCore(UserVO userVo, JSONObject jsonObject) throws XbbException {
        DdPaymentSheetStandardActionPojo ddPaymentSheetStandardPojo = JSONObject.toJavaObject(jsonObject, DdPaymentSheetStandardActionPojo.class);
        PaymentSheetStandardFormatPojo data = ddPaymentSheetStandardPojo.getData();
        Long ddDataGmtModified = ddPaymentSheetStandardPojo.getDdDataGmtModified();
        Long modifiedTime = ddDataGmtModified/1000L;
        String serialNo = data.getCode();
        List<String> serialNoList = new ArrayList<>();
        serialNoList.add(serialNo);
        List<PaasFormDataEntityExt> paasFormDataEntityExtList = getPaymentSheetBySerialNos(userVo.getCorpid(), serialNoList);
        List<Long> dataIdList = new ArrayList<>();
        for (PaasFormDataEntityExt paasFormData : paasFormDataEntityExtList) {
            if (paasFormData.getUpdateTime() < modifiedTime) {
                dataIdList.add(paasFormData.getDataId());
            } else {
                LOG.info("存在回款单，但回款单的更新时间>=执行动作接收到的更新时间 id:" + paasFormData.getDataId());
            }
        }
        PaasFormEntityExt paasForm = paasFormModel.getByBusinessType(XbbRefTypeEnum.PAYMENT_SHEET.getCode(), userVo.getCorpid());
        FormDataDeleteBatchDTO formDataDeleteBatchDTO = new FormDataDeleteBatchDTO();
        formDataDeleteBatchDTO.setLoginUser(userVo);
        formDataDeleteBatchDTO.setPlatform(PlatFormEnum.WEB.getValue());
        formDataDeleteBatchDTO.setDataIdList(dataIdList);
        formDataDeleteBatchDTO.setAppId(paasForm.getAppId());
        formDataDeleteBatchDTO.setMenuId(paasForm.getMenuId());
        formDataDeleteBatchDTO.setFormId(paasForm.getId());
        formDataDeleteBatchDTO.setSaasMark(SaasMarkEnum.SAAS.getCode());
        formDataDeleteBatchDTO.setBusinessType(XbbRefTypeEnum.PAYMENT_SHEET.getCode());
        formDataDeleteBatchDTO.setIsSkipPermissionCheck(BasicConstant.ONE);
        listBatchService.deleteBatch(formDataDeleteBatchDTO);
    }

    /**
     * 通过编号获取回款单
     * @param corpid
     * @param serialNo
     * @return
     * @throws XbbException
     */
    private PaasFormDataEntityExt getPaymentSheetBySerialNo(String corpid, String serialNo) throws XbbException {
        List<String> serialNoList = new ArrayList<>();
        serialNoList.add(serialNo);
        List<PaasFormDataEntityExt> paasFormDataEntityExtList = getPaymentSheetBySerialNos(corpid, serialNoList);
        if (paasFormDataEntityExtList.size() > 0) {
            return paasFormDataEntityExtList.get(0);
        }
        return null;
    }

    /**
     * 通过编号获取回款单
     * @param corpid
     * @param serialNoList
     * @return
     * @throws XbbException
     */
    private List<PaasFormDataEntityExt> getPaymentSheetBySerialNos(String corpid, List<String> serialNoList) throws XbbException {
        LOG.info("getPaymentSheetBySerialNos");
        BoolQueryBuilder boolQueryBuilder = boolQuery();
        boolQueryBuilder.filter(termQuery("corpid.keyword", corpid));
        boolQueryBuilder.filter(termsQuery(PaymentSheetEnum.SHEET_NO.getAttr() + ".keyword", serialNoList));
        boolQueryBuilder.filter(termQuery(StringConstant.DEL, DelEnum.NORMAL.getDel()));
        List<String> fieldList = new ArrayList<>();
        fieldList.add(StringConstant.DATA_ID);
        fieldList.add(StringConstant.CORPID);
        fieldList.add("serialNo");
        fieldList.add("appId");
        fieldList.add("menuId");
        fieldList.add("formId");
        fieldList.add("updateTime");
        List<PaasFormDataEntityExt> paasFormDataEntityExtList = esHelper.findByScroll(IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET,boolQueryBuilder,PaasFormDataEntityExt.class,fieldList);
        LOG.info("paasFormDataEntityExtList:" + paasFormDataEntityExtList.size());
        return paasFormDataEntityExtList;
    }

    private void jindieOrderSet(List<OrderSettlementPojo> orderSettlementList, Double amount) {
        Double remainAmount = amount;
        for (OrderSettlementPojo orderSettlement : orderSettlementList) {
            String settlementAmountStr = orderSettlement.getSettlementAmount();
            Double settlementAmount = Double.parseDouble(settlementAmountStr);
            Double orderPaymentAmount = 0D;
            if (remainAmount > settlementAmount) {
                orderPaymentAmount = settlementAmount;
            } else {
                orderPaymentAmount = remainAmount;
            }
            remainAmount = remainAmount - orderPaymentAmount;
            orderSettlement.setOrderPaymentAmount(orderPaymentAmount.toString());
        }
    }
}
